﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace Entangle.Resources.Windows
{
    /// <summary>
    /// Interaction logic for InfoWindow.xaml
    /// </summary>
    public partial class InfoWindow : Window
    {
        private Timer timer, fader;
        private String Message;
        private int SecondsToLive;

        public InfoWindow(string message, int secondsToLive)
        {
            Message = message;
            SecondsToLive = secondsToLive;
            timer = new Timer(new TimerCallback(this.BeginClosing), null, Timeout.Infinite, secondsToLive * 1000);

            this.Initialized += new EventHandler(InfoWindow_Initialized);
            this.MouseEnter += new MouseEventHandler(InfoWindow_MouseEnter);
            this.MouseLeave += new MouseEventHandler(InfoWindow_MouseLeave);

            InitializeComponent();
        }

        void InfoWindow_MouseLeave(object sender, MouseEventArgs e)
        {
            timer.Change(0, SecondsToLive * 1000);
        }

        void InfoWindow_MouseEnter(object sender, MouseEventArgs e)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        void InfoWindow_Initialized(object sender, EventArgs e)
        {
            txtInfo.Text = Message;
            this.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - this.Width - 10;
            this.Top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - this.Height - 10;
            timer.Change(SecondsToLive * 1000, SecondsToLive * 1000);
        }

        public void move_window(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Button_MouseEnter(object sender, MouseEventArgs e)
        {
            closeButtonPen.Brush = new SolidColorBrush(Colors.Orange);
        }

        private void Button_MouseLeave(object sender, MouseEventArgs e)
        {
            closeButtonPen.Brush = new SolidColorBrush(Colors.Black);
        }

        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void BeginClosing(object state)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            fader = new Timer(new TimerCallback(this.Fade), null, 0, 15);
        }
        private void DoFade()
        {
            if (this.Opacity > 0)
            {
                this.Opacity -= 0.01;
            }
            else
                Close();
        }
        private void Fade(object state)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(() => DoFade()));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
